Option Explicit On 

Module Functions

   '''''''''''''''''''''''''''''''''''''''''
   ' SQL Formatting Functions
   '''''''''''''''''''''''''''''''''''''''''
   Function foms(ByVal str As String) As String
      If IsDBNull(str) Then
         foms = "null"
      ElseIf Len(Trim(str)) = 0 Then
         foms = "null"
      Else
         ' Add quotes to strings for SQL Statements
         foms = "'" & Replace(CStr(str), "'", "''") & "'"
      End If
   End Function

   Function fomslen(ByVal str As String, ByVal length As Integer) As String
      Return foms(Left(str, length))
   End Function

   Function fomn(ByVal num As Long) As String
      ' Ensure null numbers go as NULL for SQL Statements
      If IsDBNull(num) Then
         fomn = "null"
      ElseIf Len(Trim(num)) = 0 Then
         fomn = "null"
      ElseIf IsNumeric(num) Then
         fomn = num
      Else
         fomn = "null"
      End If
   End Function


   '''''''''''''''''''''''''''''''''''''''''
   ' CSV Functions
   '''''''''''''''''''''''''''''''''''''''''
   Function fomcsv(ByVal str As String) As String
      ' Format a csv list for saving
      If Len(str) > 0 Then
         str = Trim(str)
         str = Replace(str, ", ", ",")
         str = Replace(str, " ,", ",")
         If str <> "" Then
            If Left(str, 1) <> "," Then str = "," & str
            If Right(str, 1) <> "," Then str = str & ","
         End If
         Return str
      Else
         Return ""
      End If
   End Function

   Function CSVInStr(ByVal values As String, ByVal value As String) As Boolean
      ' Checks If value appears in the CSV list values
      Dim a, b As String
      a = CStr(fomcsv(values))
      b = "," & CStr(Trim(value)) & ","
      Return (InStr(a, b) > 0)
   End Function


   '''''''''''''''''''''''''''''''''''''''''
   ' String Functions
   '''''''''''''''''''''''''''''''''''''''''

   Function Before(ByVal strData As String, ByVal strStart As String) As String
      ' Get string data before a substring (case sensitive)

      Dim t1 As Integer = InStr(strData, strStart)
      If t1 > 0 Then
         Return Left(strData, t1 - 1)
      Else
         Return ""
      End If

   End Function

   Function After(ByVal strData As String, ByVal strStart As String) As String
      ' Get string data after a substring (case sensitive)

      Dim t1 As Integer = InStr(strData, strStart)
      If t1 > 0 Then
         Return Mid(strData, t1 + Len(strStart))
      Else
         Return ""
      End If

   End Function

   Function Between(ByRef strData As String, ByVal strStart As String, ByVal strEnd As String) As String
      ' Get string data between 2 substrings (case sensitive)

      Dim t1 As Integer = InStr(strData, strStart)
      If t1 > 0 Then
         Dim t2 As Integer = InStr(t1 + Len(strStart), strData, strEnd)
         If t1 > 0 And t2 > 0 And t2 > t1 Then
            Return Mid(strData, t1 + Len(strStart), t2 - (t1 + Len(strStart)))
         Else
            Return ""
         End If
      Else
         Return ""
      End If

   End Function

   '''''''''''''''''''''''''''''''''''''''''
   ' String Functions
   '''''''''''''''''''''''''''''''''''''''''
   Function FormatDateTimeSec(ByVal dt As DateTime) As String
      Dim temp As String
      temp = Year(dt) & "-" & Right("0" & Month(dt), 2) & "-" & Right("0" & Day(dt), 2) & " " & Right("0" & Hour(dt), 2) & ":" & Right("0" & Minute(dt), 2) & ":" & Right("0" & Second(dt), 2)
      If Right(temp, 9) = " 00:00:00" Then temp = Left(temp, Len(temp) - 9)
      Return temp
   End Function

   Function FormatDateTime(ByVal dt As DateTime) As String
      Dim temp As String
      temp = Year(dt) & "-" & Right("0" & Month(dt), 2) & "-" & Right("0" & Day(dt), 2) & " " & Right("0" & Hour(dt), 2) & ":" & Right("0" & Minute(dt), 2)
      If Right(temp, 6) = " 00:00" Then temp = Left(temp, Len(temp) - 6)
      Return temp
   End Function

   Function ReadBinaryStream(ByVal s As System.IO.Stream, ByVal Size As Integer) As Byte()
      ' Read bytes from Stream into byte array
      Dim BReader As New System.IO.BinaryReader(s)
      Return BReader.ReadBytes(Size)
   End Function

   Sub WriteBinaryStream(ByVal Filename As String, ByVal data As Byte())
      ' Write bytes to a file (overwrite if exists, create if not)
      Dim fs As New IO.FileStream(Filename, IO.FileMode.Create)
      fs.Write(data, 0, data.Length)
      fs.Close()
   End Sub

   Function ParseInt(ByVal s As String, Optional ByVal def As Integer = 0) As Integer
      Try
         Return Integer.Parse(s)
      Catch ex As Exception
         Return def
      End Try
   End Function

   Function ParseDate(ByVal s As String, ByVal def As Date) As Date
      Try
         Return Date.Parse(s)
      Catch ex As Exception
         Return def
      End Try
   End Function

End Module
